home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / XML / fo2pdf.php < prev    next >
PHP Script  |  2004-03-24  |  10KB  |  339 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group                                |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license,       |
  8. // | that is bundled with this package in the file LICENSE, and is        |
  9. // | available at through the world-wide-web at                           |
  10. // | http://www.php.net/license/2_02.txt.                                 |
  11. // | If you did not receive a copy of the PHP license and are unable to   |
  12. // | obtain it through the world-wide-web, please send a note to          |
  13. // | license@php.net so we can mail you a copy immediately.               |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Christian Stocker <chregu@phant.ch>                         |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: fo2pdf.php,v 1.10 2003/03/20 15:32:16 arnaud Exp $
  19.  
  20. /**
  21.  * Required files
  22.  */
  23. require_once 'PEAR.php';
  24.  
  25. /**
  26.  * FO to pdf converter.
  27.  *
  28.  * with fo (formating objects) it's quite easy to convert xml-documents into
  29.  *  pdf-docs (and not only pdf, but also ps, pcl, txt and more)
  30.  *
  31.  * see README.fo2pdf for details
  32.  *
  33.  * @author   Christian Stocker <chregu@nomad.ch>
  34.  * @version  $Id: fo2pdf.php,v 1.10 2003/03/20 15:32:16 arnaud Exp $
  35.  * @package  XML
  36.  */
  37. class XML_fo2pdf
  38. {
  39.     /**
  40.      * fo-file used in this class
  41.      *
  42.      * @var  string
  43.      */
  44.     var $fo = '';
  45.  
  46.     /**
  47.      * pdf-file used in this class
  48.      *
  49.      * @var  string
  50.      */
  51.     var $pdf = '';
  52.  
  53.     /**
  54.      * Where the temporary fo and pdf files should be stored
  55.      *
  56.      * @var  string
  57.      */
  58.     var $tmpdir = '/tmp';
  59.  
  60.     /**
  61.      * A prefix for the temporary files
  62.      *
  63.      * @var  string
  64.      */
  65.     var $tmppdfprefix = 'pdffo';
  66.  
  67.     /**
  68.      * the render Type. At the moment (fop 0.20.1), possible values are
  69.      * - awt  
  70.      * - mif
  71.      * - pcl
  72.      * - pdf
  73.      * - ps  
  74.      * - txt 
  75.      * - xml
  76.      *
  77.      * @var string
  78.      */
  79.     var $renderer = 'pdf';
  80.  
  81.     /**
  82.      * the content-type to be sent if printPDF is called.
  83.      *
  84.      * @var contenttype
  85.      * @see printPDF()
  86.      */
  87.     var $contenttype = 'application/pdf';
  88.  
  89.     /** 
  90.      * If you need more Fonts or have some other stuff, which needs a
  91.      *  Fop-Configfile, you can assign one
  92.      *
  93.      * See http://xml.apache.org/fop/fonts.html for Details about
  94.      *  embedding fonts.
  95.      *
  96.      * @var configFile
  97.      */
  98.     var $configFile = null;
  99.  
  100.       
  101.     /**
  102.      * constructor
  103.      * ATTENTION (you've been warned!):
  104.      * You should not pass the values here, 'cause then you don't have
  105.      *  Error-Reporting. This variables are only here for Backwards Compatibilty..
  106.      *  Use $fop->run("input.fo","output.pdf") instead.
  107.      *
  108.      * @param    string  $fo     file input fo-file (do not use it anymore)
  109.      * @param    string  $pdf    file output pdf-file (do not use it anymore)
  110.      * @see run(), runFromString(), runFromFile()
  111.      * @access public
  112.      */
  113.     function xml_fo2pdf ($fo = null, $pdf = '')
  114.     {
  115.         if (!(is_null($fo))) {
  116.            $this->run($fo, $pdf);
  117.         }
  118.     }
  119.  
  120.     /**
  121.      * Calls the Main Fop-Java-Programm
  122.      *
  123.      * One has to pass an input fo-file
  124.      *  and if the pdf should be stored permanently, a filename/path for
  125.      *  the pdf.
  126.      *  if the pdf is not passed or empty/false, a temporary pdf-file
  127.      *   will be created
  128.      *
  129.      * @param    string  $fo     file input fo-file
  130.      * @param    string  $pdf    file output pdf-file
  131.      * @param    boolean $DelFo  if the fo should be deleted after execution
  132.      * @see runFromString()
  133.      */
  134.     function run($fo, $pdf = '', $DelFo = false)
  135.     {
  136.         $returnuri = false;
  137.         if (!$pdf) {
  138.             $pdf = tempnam($this->tmpdir, $this->tmppdfprefix);
  139.             $returnuri = true;
  140.         }
  141.  
  142.         $this->pdf = $pdf;
  143.         $this->fo  = $fo;
  144.         $options   = array();
  145.         //$options   = array('-d');
  146.         if ($this->configFile) {
  147.             $options = array('-c', $this->configFile);
  148.             //array_push($options, '-c', $this->configFile);
  149.         }
  150.         
  151.         array_push($options, $this->fo, '-' . $this->renderer, $this->pdf);
  152.  
  153.         if (!$options = @new Java("org.apache.fop.apps.CommandLineOptions", $options)) {
  154.             return PEAR::raiseError('Unable to create Java Virtual Machine in ' . __FILE__ . ':' . __LINE__, 11, PEAR_ERROR_RETURN, null, null);
  155.         } // if
  156.  
  157.         $starter = $options->getStarter();
  158.         $starter->run();
  159.  
  160.         if ($DelFo) {
  161.             $this->deleteFo($fo);
  162.         }
  163.    
  164.         if ($returnuri) {
  165.             return $pdf;
  166.         } else {
  167.             return true;
  168.         } // if
  169.     }
  170.  
  171.     /**
  172.      * If the fo is a string, not a file, use this.
  173.      *
  174.      * If you generate the fo dynamically (for example with a
  175.      *  xsl-stylesheet), you can use this method
  176.      *
  177.      * The Fop-Java program needs a file as an input, so a
  178.      *  temporary fo-file is created here (and will be deleted
  179.      *  in the run() function.)
  180.      *
  181.      * @param    string  $fostring   fo input fo-string
  182.      * @param    string  $pdf        file output pdf-file
  183.      * @see run()
  184.      */
  185.     function runFromString($fostring, $pdf = '')
  186.     {
  187.         $fo = tempnam($this->tmpdir, $this->tmppdfprefix);
  188.         $fp = fopen($fo, 'w+');
  189.         fwrite($fp, $fostring);
  190.         fclose($fp);
  191.         return $this->run($fo, $pdf, true);
  192.     }
  193.     /**
  194.      * A wrapper to run for better readabilty
  195.      *
  196.      * This method just calls run....
  197.      *
  198.      * @param    string  $fo     fo input fo-string
  199.      * @param    string  $pdf    file output pdf-file
  200.      * @see run()
  201.      */
  202.    function runFromFile($fo, $pdf = '')
  203.     {
  204.         return $this->run($fo, $pdf);
  205.     }
  206.  
  207.     /**
  208.      * Deletes the created pdf
  209.      *
  210.      * If you dynamically create pdfs and you store them
  211.      *  for example in a Cache, you don't need it afterwards.
  212.      * If no pdf is given, the one generated in run() is deleted
  213.      *
  214.      * @param    string  $pdf    file output pdf-file
  215.      * @access public
  216.      */
  217.     function deletePDF($pdf = '')
  218.     {
  219.         if (!$pdf) {
  220.             $pdf = $this->pdf;
  221.         }
  222.  
  223.         @unlink($pdf);
  224.     }
  225.  
  226.     /**
  227.      * Deletes the created fo
  228.      *
  229.      * If you dynamically create fos, you don't need it afterwards.
  230.      * If no fo-file is given, the one generated in run() is deleted
  231.      *
  232.      * @param    string  $fo  file input fo-file
  233.      */
  234.     function deleteFo($fo = '')
  235.     {
  236.         if (!$fo) {
  237.             $fo = $this->fo;
  238.         }
  239.  
  240.         @unlink($fo);
  241.     }
  242.  
  243.     /**
  244.      * Prints the content header and the generated pdf to the output
  245.      *
  246.      * If you want to dynamically generate pdfs and return them directly
  247.      *  to the browser, use this.
  248.      * If no pdf-file is given, the generated from run() is taken.
  249.      *
  250.      * @param    string  $pdf    file output pdf-file
  251.      * @see returnPDF()
  252.      * @access public    
  253.      */
  254.     function printPDF($pdf = '')
  255.     {
  256.         $pdf = $this->returnPDF($pdf);
  257.         Header('Content-type: ' . $this->contenttype . "\r\nContent-Length: " . strlen($pdf));
  258.         print $pdf;
  259.     }
  260.  
  261.     /**
  262.      * Returns the pdf
  263.      *
  264.      * If no pdf-file is given, the generated from run() is taken.
  265.      *
  266.      * @param    string  $pdf    file output pdf-file
  267.      * @return   string pdf
  268.      * @see run()    
  269.      */
  270.     function returnPDF($pdf = '')
  271.     {
  272.         if (!$pdf) {
  273.             $pdf = $this->pdf;
  274.         }
  275.  
  276.         $fd = fopen($pdf, "r");
  277.         $content = fread( $fd, filesize($pdf) );
  278.         fclose($fd);
  279.         return $content;
  280.     }
  281.     
  282.     /**
  283.      * sets the rendertype
  284.      *
  285.      * @param    string  $renderer    the type of renderer which should be used
  286.      * @param    string  $overwriteContentType if the contentType should be set to a approptiate one    
  287.      * @see $renderer
  288.      * @access public
  289.      */  
  290.     
  291.     function setRenderer($renderer = 'pdf', $overwriteContentType = true)
  292.     {
  293.         $this->renderer = $renderer;
  294.         if ($overwriteContentType) {
  295.             switch ($renderer) {        
  296.                     case 'pdf':
  297.                     $this->contenttype = 'application/pdf';
  298.                     break;
  299.                     case 'ps':
  300.                     $this->contenttype = 'application/ps';
  301.                     break;
  302.                     case 'pcl':
  303.                     $this->contenttype = 'application/pcl';
  304.                     break;                    
  305.                     case 'txt':
  306.                     $this->contenttype = 'text/plain';
  307.                     break;                                        
  308.                     case 'xml':
  309.                     $this->contenttype = 'text/xml';
  310.                     break;                                        
  311.              }
  312.          }
  313.     }
  314.  
  315.     /**
  316.      * sets the content-type
  317.      *
  318.      * @param string $contenttype the content-type for the http-header
  319.      * @see $contenttype
  320.      * @access public
  321.      */  
  322.     function setContentType($contenttype = 'application/pdf')
  323.     {
  324.         $this->contenttype = $contenttype;
  325.     }
  326.  
  327.     /**
  328.      * Sets the configfile-type.
  329.      *
  330.      * @param   string $configFile the config file for fop 
  331.      * @access  public
  332.      * @see     $configFile
  333.      */  
  334.     function setConfigFile($configFile)
  335.     {
  336.         $this->configFile = $configFile;
  337.     }
  338. }
  339. ?>